#ifndef BATTERY_YJT_H
#define BATTERY_YJT_H

#include <ICANCmd.h>

#include <QDebug>
#include <QObject>

#include "QTimer"

class Battery_YJT : public QObject {
  Q_OBJECT
 public:
  enum ChargeState { Uncharge = 0, Charging = 1, Charged = 2 };
  Q_ENUM(ChargeState)

  explicit Battery_YJT(QObject* parent = nullptr);

  bool CanOpenAndStart();                            //打开电池CAN设备
  bool CanStopAndClose();                            //关闭电池CAN设备
  bool CanReceiveData();                             // CAN接收电池数据
  void CanSendData(PCAN_DataFrame frames, ULONG i);  // CAN发送电池数据
  void RegularGetData();                             //定期获取电池CAN数据

  //外部调用充电函数
  void charge(bool enable);  //电池开启充电

  struct BatteryData_YJT {
    double total_vol{0};                 //电池总电压
    double total_cur{0};                 //电池总电流
    double soc{0};                       //电池SOC
    double max_singel_vol{0};            //最高单体电压
    uint16_t max_singel_vol_num{0};      //最高单体电压串号
    double min_singel_vol{0};            //最低单体电压
    uint16_t min_singel_vol_num{0};      //最低单体电压串号
    int16_t max_temp{0};                 //电池最高温度
    uint16_t max_temp_num{0};            //电池最高温度编号
    int16_t min_temp{0};                 //电池最低温度
    uint16_t min_temp_num{0};            //电池最低温度编号
    bool charge_relay_state{false};      //电池充电继电器状态
    ChargeState charge_state{Uncharge};  //电池充电状态
  };

  BatteryData_YJT getBatteryData() const;

 private:
  DWORD dw_Index = 0;    //第一个设备
  DWORD dw_Channel = 1;  //第一个通道
  DWORD dw_DeviceHandle;
  // BTR0   0014 -1M 0016-800K 001C-500K 011C-250K 031C-125K 041C-100K 091C-50K 181C-20K 311C-10K BFFF-5K
  BYTE dw_Btr0 = 0x03;
  BYTE dw_Btr1 = 0x1c;
  bool is_open_can = false;  // CAN设备是否打开

  BatteryData_YJT battery_data;

 signals:
  void sigShowCanData(const QByteArray& tips, const QString& string);
  void sigSendTips(const QString);
  void sigLoseConnect(const QString&);

 public slots:

  void sltCanBatteryInit();
  void sltTimeout_battery();
};

#endif  // BATTERY_YJT_H
